package cz.muni.fi.pa165.ws.adapter.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;

import cz.muni.fi.pa165.sme.api.BurnedCaloriesService;
import cz.muni.fi.pa165.sme.api.to.BurnedCaloriesTO;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.BurnedCalories;
import cz.muni.fi.pa165.ws.adapter.IBurnedCaloriesAdapter;
import cz.muni.fi.pa165.ws.converter.BurnedCaloriesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * @author forman
 */
@Component
public class BurnedCaloriesAdapter implements IBurnedCaloriesAdapter {

	@Autowired
	private BurnedCaloriesService burnedCaloriesService;

	@Autowired
	private BurnedCaloriesConverter converter;

	@Override
	public void createBurnedCalories(final BurnedCalories burnedCalories) {
		final BurnedCaloriesTO burnedCaloriesTO = converter.convert2BurnedCaloriesTO(burnedCalories);
		burnedCaloriesService.createBurnedCalories(burnedCaloriesTO);
	}

	@Override
	public void deleteBurnedCalories(final BurnedCalories burnedCalories) {
		final BurnedCaloriesTO burnedCaloriesTO = converter.convert2BurnedCaloriesTO(burnedCalories);
		burnedCaloriesService.deleteBurnedCalories(burnedCaloriesTO);
	}

	@Override
	public void updateBurnedCalories(final BurnedCalories burnedCalories) {
		final BurnedCaloriesTO burnedCaloriesTO = converter.convert2BurnedCaloriesTO(burnedCalories);
		burnedCaloriesService.updateBurnedCalories(burnedCaloriesTO);
	}

	@Override
	public BurnedCalories findBurnedCalories(final Long id) {
		final BurnedCaloriesTO burnedCalories = burnedCaloriesService.findBurnedCalories(id);
		return converter.convert2BurnedCalories(burnedCalories);
	}

	@Override
	public List<BurnedCalories> findAllBurnedCalories() {
		final List<BurnedCaloriesTO> allBurnedCalories = burnedCaloriesService.findAllBurnedCalories();
		final List<BurnedCalories> result = new ArrayList<BurnedCalories>();
		for (BurnedCaloriesTO burnedCaloriesTO : allBurnedCalories) {
			result.add(converter.convert2BurnedCalories(burnedCaloriesTO));
		}
		return result;
	}

	@PostConstruct
	private void init() {
		final List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("ADMIN"), new SimpleGrantedAuthority("USER"));
		final UserDetails userDetails = new User("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918", authorities);
		Authentication authentication = new AnonymousAuthenticationToken("ws", userDetails, authorities);
		SecurityContextHolder.getContext().setAuthentication(authentication);
	}
}
